/**
 * Sample Skeleton for "VentanaTerceraFXML.fxml" Controller Class You can copy
 * and paste this code into your favorite IDE
 *
 */
package ventanas;

import bbdd.classes.TRiesgo;
import bbdd.classes.TrutaMae;
import bbdd.sqlite.ConexionSqlLite;
import classes.Imagen;
import classes.Obstaculo;
import ficheros.ImageFileFiltrer;
import ficheros.UtilidadesFicheros;
import java.io.File;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.stage.DirectoryChooser;
import javafx.util.Callback;
import name.antonsmirnov.javafx.dialog.Dialog;

public class VentanaTerceraFXMLController
        implements Initializable {

    ConexionSqlLite connSqlite;
    @FXML //  fx:id="botonCancelar"
    private Button botonCancelar; // Value injected by FXMLLoader
    @FXML //  fx:id="botonCargarImagenes"
    private Button botonCargarImagenes; // Value injected by FXMLLoader
    @FXML //  fx:id="botonDirectorioExplorar"
    private Button botonDirectorioExplorar; // Value injected by FXMLLoader
    @FXML //  fx:id="botonInsertarRiesgo"
    private Button botonInsertarRiesgo; // Value injected by FXMLLoader
    @FXML //  fx:id="boxCurrentLat"
    private TextField boxCurrentLat; // Value injected by FXMLLoader
    @FXML //  fx:id="boxCurrentLon"
    private TextField boxCurrentLon; // Value injected by FXMLLoader
    @FXML //  fx:id="boxDataStamp"
    private TextField boxDataStamp; // Value injected by FXMLLoader
    @FXML //  fx:id="boxDirectorio"
    private TextField boxDirectorio; // Value injected by FXMLLoader
    @FXML //  fx:id="boxEw"
    private TextField boxEw; // Value injected by FXMLLoader
    @FXML //  fx:id="boxRuta"
    private ComboBox<TrutaMae> boxRuta; // Value injected by FXMLLoader
    @FXML //  fx:id="boxTimeStamp"
    private TextField boxTimeStamp; // Value injected by FXMLLoader
    @FXML //  fx:id="boxnS"
    private TextField boxnS; // Value injected by FXMLLoader
    @FXML //  fx:id="colCurrentLat"
    private TableColumn<TRiesgo, String> colCurrentLat; // Value injected by FXMLLoader
    @FXML //  fx:id="colCurrentLon"
    private TableColumn<TRiesgo, String> colCurrentLon; // Value injected by FXMLLoader
    @FXML //  fx:id="colDataStamp"
    private TableColumn<TRiesgo, String> colDataStamp; // Value injected by FXMLLoader
    @FXML //  fx:id="colIdRiesgo"
    private TableColumn<TRiesgo, Integer> colIdRiesgo; // Value injected by FXMLLoader
    @FXML //  fx:id="colIdRuta"
    private TableColumn<TRiesgo, Integer> colIdRuta; // Value injected by FXMLLoader
    @FXML //  fx:id="colNs"
    private TableColumn<TRiesgo, String> colNs; // Value injected by FXMLLoader
    @FXML //  fx:id="colTimeStamp"
    private TableColumn<TRiesgo, String> colTimeStamp; // Value injected by FXMLLoader
    @FXML //  fx:id="coleW"
    private TableColumn<TRiesgo, String> coleW; // Value injected by FXMLLoader
    @FXML //  fx:id="rootPane"
    private AnchorPane rootPane; // Value injected by FXMLLoader
    @FXML //  fx:id="tablaDatos"
    private TableView<TRiesgo> tablaDatos; // Value injected by FXMLLoader

    // Handler for Button[fx:id="botonCancelar"] onAction
    public void handleBotonCancelar(ActionEvent event) {
        this.rootPane.getScene().getWindow().hide();
    }

    // Handler for Button[fx:id="botonCargarImagenes"] onAction
    public void handleBotonCargarImagenes(ActionEvent event) {
        TRiesgo elegido = tablaDatos.getSelectionModel().getSelectedItem();
        if (elegido != null) {
            if (!boxDirectorio.getText().isEmpty()) {

                File directorio = new File(boxDirectorio.getText());
                List<Imagen> listaImagenes = UtilidadesFicheros.cargarDatos(directorio);
                /*
                 File[] listaFicheros = directorio.listFiles(new ImageFileFiltrer());
                 for (File aux : listaFicheros) {
                 System.out.println(aux.getName());
                 System.out.println(aux.getParent());
                 System.out.println(aux.getParent()+File.separator+aux.getName().substring(0, aux.getName().length()-4));
                 }*/
                
                /*for(Imagen aux :listaImagenes){
                    System.out.println(aux);
                    for(Obstaculo aux2: aux.getListaObstaculos()){
                        System.out.println(aux2);
                    }
                }*/
                 
                //Dialog.showInfo("Informacion", "Se van a cargar " + listaImagenes.size() + " Imagenes");
                if (!connSqlite.insertarImagenes(listaImagenes, elegido.getIdRiesgo())) {
                    Dialog.showError("Información", "Error al insertar las imagenes");
                } else {
                    Dialog.showInfo("Información", "Las " + listaImagenes.size() + " Imagenes insertadas con existo");
                }
            } else {
                Dialog.showWarning("Información", "Debe seleccionar un directorio");
            }
        } else {
            Dialog.showWarning("Información", "Debe seleccionar riesgo");
        }


    }

    // Handler for Button[fx:id="botonDirectorioExplorar"] onAction
    public void handleBotonDirectorioExplorar(ActionEvent event) {
        File fichero;
        DirectoryChooser directoryChooser = new DirectoryChooser();
        directoryChooser.setTitle("Elija el directorio de imagenes");
        //fichero = fileChooser.getInitialDirectory();
        //Establecemos el filtro de extension

        fichero = directoryChooser.showDialog(null);
        if (fichero != null) {
            boxDirectorio.setText(fichero.getAbsolutePath());
        } else {
            boxDirectorio.setText("");
        }
    }

    // Handler for Button[fx:id="botonInsertarRiesgo"] onAction
    public void handleBotonInsertarRiesgo(ActionEvent event) {
        // handle the event here
    }

    @Override // This method is called by the FXMLLoader when initialization is complete
    public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
        assert botonCancelar != null : "fx:id=\"botonCancelar\" was not injected: check your FXML file 'VentanaTerceraFXML.fxml'.";
        assert botonCargarImagenes != null : "fx:id=\"botonCargarImagenes\" was not injected: check your FXML file 'VentanaTerceraFXML.fxml'.";
        assert botonDirectorioExplorar != null : "fx:id=\"botonDirectorioExplorar\" was not injected: check your FXML file 'VentanaTerceraFXML.fxml'.";
        assert botonInsertarRiesgo != null : "fx:id=\"botonInsertarRiesgo\" was not injected: check your FXML file 'VentanaTerceraFXML.fxml'.";
        assert boxCurrentLat != null : "fx:id=\"boxCurrentLat\" was not injected: check your FXML file 'VentanaTerceraFXML.fxml'.";
        assert boxCurrentLon != null : "fx:id=\"boxCurrentLon\" was not injected: check your FXML file 'VentanaTerceraFXML.fxml'.";
        assert boxDataStamp != null : "fx:id=\"boxDataStamp\" was not injected: check your FXML file 'VentanaTerceraFXML.fxml'.";
        assert boxDirectorio != null : "fx:id=\"boxDirectorio\" was not injected: check your FXML file 'VentanaTerceraFXML.fxml'.";
        assert boxEw != null : "fx:id=\"boxEw\" was not injected: check your FXML file 'VentanaTerceraFXML.fxml'.";
        assert boxRuta != null : "fx:id=\"boxRuta\" was not injected: check your FXML file 'VentanaTerceraFXML.fxml'.";
        assert boxTimeStamp != null : "fx:id=\"boxTimeStamp\" was not injected: check your FXML file 'VentanaTerceraFXML.fxml'.";
        assert boxnS != null : "fx:id=\"boxnS\" was not injected: check your FXML file 'VentanaTerceraFXML.fxml'.";
        assert colCurrentLat != null : "fx:id=\"colCurrentLat\" was not injected: check your FXML file 'VentanaTerceraFXML.fxml'.";
        assert colCurrentLon != null : "fx:id=\"colCurrentLon\" was not injected: check your FXML file 'VentanaTerceraFXML.fxml'.";
        assert colDataStamp != null : "fx:id=\"colDataStamp\" was not injected: check your FXML file 'VentanaTerceraFXML.fxml'.";
        assert colIdRiesgo != null : "fx:id=\"colIdRiesgo\" was not injected: check your FXML file 'VentanaTerceraFXML.fxml'.";
        assert colIdRuta != null : "fx:id=\"colIdRuta\" was not injected: check your FXML file 'VentanaTerceraFXML.fxml'.";
        assert colNs != null : "fx:id=\"colNs\" was not injected: check your FXML file 'VentanaTerceraFXML.fxml'.";
        assert colTimeStamp != null : "fx:id=\"colTimeStamp\" was not injected: check your FXML file 'VentanaTerceraFXML.fxml'.";
        assert coleW != null : "fx:id=\"coleW\" was not injected: check your FXML file 'VentanaTerceraFXML.fxml'.";
        assert rootPane != null : "fx:id=\"rootPane\" was not injected: check your FXML file 'VentanaTerceraFXML.fxml'.";
        assert tablaDatos != null : "fx:id=\"tablaDatos\" was not injected: check your FXML file 'VentanaTerceraFXML.fxml'.";

        // initialize your logic here: all @FXML variables will have been injected


    }

    void setConexiones(ConexionSqlLite connSqlite) {
        this.connSqlite = connSqlite;

        List<TRiesgo> listaRiesgos = connSqlite.getRiesgos();
        recargarTable(listaRiesgos);
    }

    private void recargarTable(List<TRiesgo> listaTrayecto) {

        this.colIdRuta.setCellValueFactory(new PropertyValueFactory<TRiesgo, Integer>("idRuta"));
        this.colIdRiesgo.setCellValueFactory(new PropertyValueFactory<TRiesgo, Integer>("idRiesgo"));
        this.colTimeStamp.setCellValueFactory(new Callback<CellDataFeatures<TRiesgo, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(CellDataFeatures<TRiesgo, String> p) {
                return new SimpleStringProperty(p.getValue().getTimeStampString());
            }
        });
        this.colCurrentLat.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<TRiesgo, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(CellDataFeatures<TRiesgo, String> p) {
                return new SimpleStringProperty(p.getValue().getCurrentLat().toString());
            }
        });
        this.colNs.setCellValueFactory(new PropertyValueFactory<TRiesgo, String>("ns"));
        this.colCurrentLon.setCellValueFactory(new Callback<CellDataFeatures<TRiesgo, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(CellDataFeatures<TRiesgo, String> p) {
                return new SimpleStringProperty(p.getValue().getCurrentLon().toString());
            }
        });
        this.coleW.setCellValueFactory(new PropertyValueFactory<TRiesgo, String>("ew"));

        this.colDataStamp.setCellValueFactory(new Callback<CellDataFeatures<TRiesgo, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(CellDataFeatures<TRiesgo, String> p) {
                return new SimpleStringProperty(p.getValue().getDataStamp().toString());
            }
        });
        ObservableList<TRiesgo> obLitaTrayecto = FXCollections.observableList(listaTrayecto);
        this.tablaDatos.setItems(obLitaTrayecto);
    }
}
